package com.kehutong.department.app;


import com.kehutong.common.Authutil.OAuth2Provider;

import com.kehutong.department.service.MessagePushService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.oauth2.OAuth2Accessor;
import org.coraframework.oauth2.OAuth2ProblemException;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;


/**
 * 第三方中海消息推动类
 * @author liulei
 */
@MvcAction
@ReqMapping("/admin/third/message/push")
public class MessagePushController {
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private MessagePushService messagePushService;

    @Inject
    private OAuth2Provider oauth2Provider;

    /**
     * 第三方中海员工消息推送
     * @author liulei
     */
    @ReqMapping("/staffMessagePush")
    public Object staffMessagePush(JSONObject params) {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return com.kehutong.common.errcode.Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return com.kehutong.common.errcode.Result.error(1002, "授权已失效");
        }
        try {
            messagePushService.ThirdMessagePush(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success();
    }

    /**
     * 第三方业主群发消息（创建企业群发）
     * @author liulei
     */
    @ReqMapping("/MassMessagePush")
    public Object MassMessagePush(JSONObject params) {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return com.kehutong.common.errcode.Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return com.kehutong.common.errcode.Result.error(1002, "授权已失效");
        }
        try {
            messagePushService.ThirdMassPush(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success();
    }
}
